﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Sustainalytics.Auth.Claims;

namespace Sustainalytics.Auth.Service.GA
{
    public static class UserExtensions
    {
        public static async Task<IEnumerable<Claim>> GetClaimsAsync(this GaUser user, GaClaimsProvider gaClaimsProvider = null)
        {
            var standardClaims = user.GetStandardClaims();
            var customClaims = user.GetCustomClaims();
            var claims = gaClaimsProvider != null
                ? await gaClaimsProvider.GetClaimsAsync(user)
                : Enumerable.Empty<Claim>();

            return standardClaims.Concat(customClaims).Concat(claims);
        }

        public static IEnumerable<Claim> GetStandardClaims(this GaUser user) => new[]
        {
            new Claim(ClaimTypes.Email, user.Email),
            new Claim(ClaimTypes.Name, user.Email),
            new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
            new Claim(ClaimTypes.GivenName, user.FirstName + " " + user.MiddleName),
            new Claim(ClaimTypes.Surname, user.LastName),
        };

        public static IEnumerable<Claim> GetCustomClaims(this GaUser user) => new[]
        {
            new Claim(CustomClaimTypes.UserId, user.Id.ToString()),
            new Claim(CustomClaimTypes.AccountName, user.AccountName),
            new Claim(CustomClaimTypes.AccountId, user.AccountId.ToString()),
            new Claim(CustomClaimTypes.AccountTier, user.AccountTier.ToString()),
            new Claim(CustomClaimTypes.AccountManagerEmail, user.AccountManagerEmail ?? "client@sustainalytics.com"),
            new Claim(CustomClaimTypes.AccountManagerFullName, user.AccountManagerName),
            new Claim(CustomClaimTypes.EsgProfileId, user.ActiveEsgProfileId.ToString()),
            new Claim("first_user_RR", user.FirstTimeRRUserAccepted.ToString()),
            new Claim("first_user_SP", user.FirstTimeSPUserAccepted.ToString()),
            new Claim("Home/dashboard", "Allow.Read"),
            new Claim("Tools", "Allow.All"),
        };
    }
}
